package helloworld.admin.service.teacher;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import helloworld.dao.TeacherDao;
import helloworld.dao.DAOException;
import helloworld.dao.DAOFactory;
import helloworld.example.exception.ErrorLevel;
import helloworld.exception.DuplicateTeacherException;

import helloworld.admin.bean.*;

/**
 * In real projects, TeacherControllerService object is often implemented as Session EJB.
 * It represents a Business Service and is Session Facade pattern (Core J2EE Patterns) 
 * 
 * @author TUYEN-DM
 * @version $Revision:   $ $Date:2009/05/13   $
 */
public class TeacherControllerService {

	public TeacherAdminBean getTeacherDetail(String teacherName) throws TeacherServiceException
	{
		TeacherAdminBean teacher = null;
		try{
			DAOFactory factory = DAOFactory.getDAOFactory(DAOFactory.MS_ACCESS);
			TeacherDao dao = factory.getTeacherDAO();
			teacher = dao.fetchDetail(teacherName);
			
		}catch (DAOException de)
		{
			if (! de.isLogged())
			{
				Log log = LogFactory.getLog("TeacherFetchService");
				de.log(log);
			}
			throw new TeacherServiceException(de.getUserMessageKey(), 
									de.getValueReplacementArray(),
									de.getUniqueID(), 
									ErrorLevel.ERROR);
		}
		
		return teacher;
	}
	
	public int createTeacher(TeacherAdminBean teacher) throws TeacherServiceException, DuplicateTeacherException{
		try{
			/*teacher.getAddress().setAddress1("N/A");
			teacher.getAddress().setAddress2("N/A");*/
			DAOFactory factory = DAOFactory.getDAOFactory(DAOFactory.MS_ACCESS);
			TeacherDao dao = factory.getTeacherDAO();
			return dao.create(teacher);
		}catch (DAOException de) {
			// TODO: handle exception
			if(!de.isLogged()){
				Log log = LogFactory.getLog("TeacherInsertService");
				de.log(log);
			}
			throw new TeacherServiceException(de.getUserMessageKey(),de.getValueReplacementArray(),
												de.getUniqueID(),ErrorLevel.ERROR);
		}
	}
	
	public void updateTeacher(TeacherAdminBean teacher)throws TeacherServiceException,DuplicateTeacherException{
		try{
			/*teacher.getAddress().setAddress1("N/A");
			teacher.getAddress().setAddress2("N/A");*/
			DAOFactory factory = DAOFactory.getDAOFactory(DAOFactory.MS_ACCESS);
			TeacherDao dao = factory.getTeacherDAO();
			dao.update(teacher);
		}catch (DAOException de) {
			// TODO: handle exception
			if(!de.isLogged()){
				Log log = LogFactory.getLog("TeacherUpdateService");
				de.log(log);
			}
			throw new TeacherServiceException(de.getUserMessageKey(),de.getValueReplacementArray(),
												de.getUniqueID(),ErrorLevel.ERROR);
		}
	}
	
	public void deleteTeacher(int id) throws TeacherServiceException{
		try{
			
			DAOFactory factory = DAOFactory.getDAOFactory(DAOFactory.MS_ACCESS);
			TeacherDao dao = factory.getTeacherDAO();
			dao.delete(id);
		}catch (DAOException de) {
			// TODO: handle exception
			if(!de.isLogged()){
				Log log = LogFactory.getLog("TeacherDeleteService");
				de.log(log);
			}
			throw new TeacherServiceException(de.getUserMessageKey(),de.getValueReplacementArray(),
												de.getUniqueID(),ErrorLevel.ERROR);
		}
	}
}
